package org.tony.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.tony.utils.DataCustomizedUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * 数据定制 实体对象
 * @author tony
 *
 */
public class DataCusomizedEntry {
	
	private String relationId ;
	private String tableName ;
	private String relationKey ;
	private List<RelationDataSourceEntry> relDts ;
	public String getRelationId() {
		return relationId;
	}
	public void setRelationId(String relationId) {
		this.relationId = relationId;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public List<RelationDataSourceEntry> getRelDts() {
		return relDts;
	}
	public void setRelDts(List<RelationDataSourceEntry> relDts) {
		this.relDts = relDts;
	}
	
	public Node generateNode(Document doc) {
		Element ele = doc.createElement("relation") ;
		ele.setAttribute("id", this.relationId);
		ele.setAttribute("table", this.tableName);
		ele.setAttribute("relation_key", this.relationKey);
		for(RelationDataSourceEntry rel : relDts) {
			if(rel.getColumns().size()>0) {
				ele.appendChild(rel.generateNode(doc));
			}
		}
		return ele ;
	}
	
	public String getRelationKey() {
		return relationKey;
	}
	public void setRelationKey(String relationKey) {
		this.relationKey = relationKey;
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("relDts", this.relDts).append(
				"tableName", this.tableName).append("relationId",
				this.relationId).toString();
	}
	
	public RelationDataSourceEntry search(String fromDs,String fromTable) {
		for(RelationDataSourceEntry rel :relDts) {
			if(rel.getFromDateSourceId().equals(fromDs) && rel.getFromTableName().equals(fromTable)) {
				return rel ;
			}
		}
		return null ;
	}
	
	public void trim(){
		List<Integer> needDel = new ArrayList<Integer>();
		DataCusomizedEntry sameDce = DataCustomizedUtil.getInstance().search(this.relationKey, this.tableName) ;
		if(sameDce!=null) {
			for(int i=0;i<relDts.size();i++) {
				RelationDataSourceEntry rel = relDts.get(i); 
				RelationDataSourceEntry temp = sameDce.search(rel.getFromDateSourceId(), rel.getFromTableName()) ;
				if(temp != null) {
					for(Iterator<Map.Entry<String, String>> iter = temp.getColumns().entrySet().iterator();iter.hasNext();) {
						String curCol = iter.next().getKey() ;
						if(rel.getColumns().containsKey(curCol)) {
							relDts.get(i).getColumns().remove(curCol) ;
						}
					}
					if(relDts.get(i).getColumns().size() == 0) {
						needDel.add(i);
					}
					
				}
			}
		}
		for(Iterator<Integer> i = needDel.iterator();i.hasNext();) {
			relDts.remove(i.next().intValue()) ;
		}
	}
	
	

}
